import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User: Administrator
 * Data: 2023 - 09 - 04
 * Time: 18:30
 */
//牛客 两数之和
public class Solution1 {
    //官方推荐方法
    public int[] twoSum (int[] numbers, int target) {
        int[] res = new int[0];
        Map<Integer, Integer> hash = new HashMap<>();//哈希表的K存储数组元素，V存储对应数组元素的下标
        for (int i = 0; i < numbers.length; i++) {
            int temp = target - numbers[i];
            if(!hash.containsKey(temp)) {
                hash.put(numbers[i], i);
            }else {
                return new int[]{hash.get(temp) + 1, i + 1};
            }
        }
        return res;
    }
}
